package com.jixin.jdpay;

import java.util.HashMap;
import java.util.Map;

import com.jixin.jdpay.core.DES;
import com.jixin.jdpay.core.FTL;
import com.jixin.jdpay.core.MD5;
import com.jixin.jdpay.core.XmlMsg;
import com.sun.org.apache.xml.internal.security.utils.Base64;

public class JdPayConfigUtil {
	
	//配置文件路径
	public static final String CONFIG_FILE_PATH			= "jdpay";
	
	private static JdPayConfig config 			= null;
	//网银在线请求接口路径
	private static String REQUEST_URL 			= "";
	//网银在线开通的商户Id
	private static String MERCHANT_ID 			= "";
	//网银在线请求的商户编码
	private static String CHARSET				= "";
	//DES加密密钥
	private static String DES_KEY				= "";
	//MD5签名私钥
	private static String MD5_KEY				= "";
	//API版本
	private static String VERSION				= "";
	//终端
	private static String TERMINAL				= "";
	//回调地址
	private static String NOTIFY_URL			= "";
	
	static{
		//初始化merchantInfo
		config					= JdPayConfig.getInstance(CONFIG_FILE_PATH);
		//请求api路径
		REQUEST_URL				= config.getValue("url");
		//从配置文件中获取商户编号
		MERCHANT_ID 			= config.getValue("merchant_id");
		//从配置文件中获取编码
		CHARSET 				= config.getValue("charset");
		//DES机密密钥
		DES_KEY					= config.getValue("des_key");
		//签名私钥
		MD5_KEY					= config.getValue("md5_key");
		//编码
		VERSION					= config.getValue("version");
		//签名加密算法
		TERMINAL				= config.getValue("terminal");
		//融宝回调
		NOTIFY_URL				= config.getValue("notifyUrl");
	}
	
	public static String getREQUEST_URL() {
		return REQUEST_URL;
	}

	public static void setREQUEST_URL(String rEQUEST_URL) {
		REQUEST_URL = rEQUEST_URL;
	}

	public static String getMERCHANT_ID() {
		return MERCHANT_ID;
	}

	public static void setMERCHANT_ID(String mERCHANT_ID) {
		MERCHANT_ID = mERCHANT_ID;
	}

	public static String getCHARSET() {
		return CHARSET;
	}

	public static void setCHARSET(String cHARSET) {
		CHARSET = cHARSET;
	}

	public static String getDES_KEY() {
		return DES_KEY;
	}

	public static void setDES_KEY(String dES_KEY) {
		DES_KEY = dES_KEY;
	}

	public static String getMD5_KEY() {
		return MD5_KEY;
	}

	public static void setMD5_KEY(String mD5_KEY) {
		MD5_KEY = mD5_KEY;
	}

	public static String getVERSION() {
		return VERSION;
	}

	public static void setVERSION(String vERSION) {
		VERSION = vERSION;
	}

	public static String getTERMINAL() {
		return TERMINAL;
	}

	public static void setTERMINAL(String tERMINAL) {
		TERMINAL = tERMINAL;
	}

	public static String getNOTIFY_URL() {
		return NOTIFY_URL;
	}

	public static void setNOTIFY_URL(String nOTIFY_URL) {
		NOTIFY_URL = nOTIFY_URL;
	}

	public static String getConfigFilePath() {
		return CONFIG_FILE_PATH;
	}
	
	/**
	 * 构建请求数据
	 * @param params
	 * @return
	 */
	public static String buildRequestParam(Map<String, Object> params){
		String reqParams = "";
		try{
			String data = FTL.doString(params,  "/xml/", "http_des_req_data.xml");
			//将请求参数XML中的data元素用DES加密,DES密钥是在商户在网银在线后台设置的
			String dataDES = DES.encrypt(data, JdPayConfigUtil.getDES_KEY(), 
					JdPayConfigUtil.getCHARSET());
			//计算数据签名version+merchant+terminal+data元素，MD5密钥是在商户在网银在线后台设置，签名是为了验证请求的合法性
			StringBuffer sb = new StringBuffer();
			sb.append(JdPayConfigUtil.getVERSION());
			sb.append(JdPayConfigUtil.getMERCHANT_ID());
			sb.append(JdPayConfigUtil.getTERMINAL());
			sb.append(dataDES);
			String signMD5 = MD5.md5(sb.toString(), JdPayConfigUtil.getMD5_KEY());
			String reqXML = getReqXML(dataDES, signMD5);
			//最后将xml用base64加密
			reqParams = Base64.encode(reqXML.getBytes());
		}catch(Exception e){
			e.printStackTrace();
		}
		return reqParams;
	}

	
	/**
	 * 处理交易结果
	 * @param respStr
	 * @return
	 */
	public static Map<String, String> operate(String respStr){
		try {
			//数据格式是resp=XML数据
			String temResp = respStr.substring(respStr.indexOf("=") + 1);
			//快捷支付数据先base64解码
			temResp = new String(Base64.decode(temResp));
			//解析xml中的数据
			Map<String, String> respParams= XmlMsg.parse(temResp);
			//验证数据签名的合法性。版本号+商户号+终端号+交易数据使用md5加密和数据签名比较，md5密钥在网银在线商户后台设置
			if(!MD5.verify(respParams.get("chinabank.version")
					+respParams.get("chinabank.merchant")+respParams.get("chinabank.terminal")
					+respParams.get("chinabank.data"), MD5_KEY, respParams.get("chinabank.sign"))){
				return null;
			}
			//使用DES解密data交易数据,des密钥网银在线商户后台设置
			String dataDES = DES.decrypt(respParams.get("chinabank.data"), DES_KEY, respParams.get("xml.charset"));
			Map<String, String> dataParams= XmlMsg.parse(dataDES);
			return dataParams;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 构建请求网银在线的xml请求数据
	 * @param dataElmtDES
	 * @param signMD5
	 * @return
	 */
	public static String getReqXML(String dataElmtDES, String signMD5){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("charset", JdPayConfigUtil.getCHARSET());
		params.put("version", JdPayConfigUtil.getVERSION());
		params.put("merchant", JdPayConfigUtil.getMERCHANT_ID());
		params.put("terminal", JdPayConfigUtil.getTERMINAL());
		params.put("data", dataElmtDES);
		params.put("sign", signMD5);
		System.out.println(params);
		return FTL.doString(params, "/xml/", "http_des_req.xml");
	}
	

}
